package cn.vetech.sync.elasticsearch.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.vetech.sync.elasticsearch.model.request.SyncByTableNoPkIntervalRequest;
import cn.vetech.sync.elasticsearch.model.request.SyncByTableRequest;
import cn.vetech.sync.elasticsearch.model.response.Response;
import cn.vetech.sync.elasticsearch.service.SyncService;
import cn.vetech.sync.elasticsearch.util.JsonUtil;

import javax.annotation.Resource;

/**
 * @author <a href="mailto:wangchao.star@gmail.com">wangchao</a>
 * @version 1.0
 * @since 2017-08-29 19:32:00
 */
@Controller
@RequestMapping("/sync")
public class SyncController {
	private static final Logger logger = LoggerFactory.getLogger(SyncController.class);

	@Resource
	private SyncService syncService;

	/**
	 * 通过库名和表名全量同步数据(需要主键为数字类型)
	 *
	 * @param request
	 *            SyncByTableRequest 请求参数
	 */
	@RequestMapping("/syncTable")
	@ResponseBody
	public String syncTable(@Validated SyncByTableRequest request) {
		logger.debug("request_info: " + JsonUtil.toJson(request));
		String response = Response.success(syncService.syncByTable(request)).toString();
		logger.debug("response_info: " + JsonUtil.toJson(request));
		return response;
	}
	/**
	 * 通过库名和表名全量同步数据(忽略主键类型)
	 *
	 * @param request
	 *            SyncByTableRequest 请求参数
	 */
	@RequestMapping("/syncTableIgnorePrimaryKey")
	@ResponseBody
	public String syncTableIgnorePrimaryKey(@Validated SyncByTableNoPkIntervalRequest request) {
		String response = Response.success(syncService.syncTableIgnorePrimaryKey(request)).toString();
		return response;
	}
	/**
	 * 通过库名和表名全量同步数据(忽略主键类型)
	 *
	 * @param request
	 *            SyncByTableRequest 请求参数
	 */
	@RequestMapping("/clean")
	@ResponseBody
	public String clean(@Validated SyncByTableNoPkIntervalRequest request) {
		String response = Response.success(syncService.clean(request)).toString();
		return response;
	}
}
